﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Slb.Ocean.Petrel.DomainObject.Seismic;
using Public.Ocean.Petrel.Extensions.Internal;

namespace Public.Ocean.Petrel.Extensions.DomainObject
{
    public static class SeismicExtensions
    {
        /// <summary>
        /// Gets all seismic cubes given a root seismic cube.
        /// </summary>
        /// <param name="root">The root.</param>
        /// <returns></returns>
        public static IEnumerable<SeismicCube> SeismicCubesRecursive(this SeismicCube root)
        {
            return Enumerations.Flatten(new[] { root }, c => c.SeismicCubes);
        }

        /// <summary>
        /// Gets all seismic collections given a root seismic collection.
        /// </summary>
        /// <param name="root">The root.</param>
        /// <returns></returns>
        public static IEnumerable<SeismicCollection> SeismicCollectionsRecursive(this SeismicCollection root)
        {
            return Enumerations.Flatten(new[] { root }, c => c.SeismicCollections);
        }

        /// <summary>
        /// Gets all seismic cubes given a root seismic collection. Calling this on 
        /// </summary>
        /// <param name="root">The root.</param>
        /// <returns></returns>
        public static IEnumerable<SeismicCube> SeismicCubesRecursive(this SeismicCollection root)
        {
            foreach (var collection in SeismicCollectionsRecursive(root))
            {
                foreach (var rootCube in collection.SeismicCubes)
                {
                    foreach (var cube in SeismicCubesRecursive(rootCube))
                    {
                        yield return cube;
                    }
                }
            }
        }

        /// <summary>
        /// Gets all seismic cubes from the seismic root.
        /// </summary>
        /// <param name="root">The root.</param>
        /// <returns></returns>
        public static IEnumerable<SeismicCube> SeismicCubesRecursive(this SeismicRoot root)
        {
            if (root.HasSeismicProject)
            {
                foreach (var collection in Enumerations.Flatten(root.SeismicProject.SeismicCollections, c => c.SeismicCollections))
                {
                    foreach (var rootCube in collection.SeismicCubes)
                    {
                        foreach (var cube in SeismicCubesRecursive(rootCube))
                        {
                            yield return cube;
                        }
                    }
                }
            }
        }
    }
}
